package com.chat.datea.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.chat.datea.R;
import com.chat.datea.adapter.SingerListViewAdapter;
import com.chat.datea.adapter.SingerViewPagerAdapter;
import com.chat.datea.bean.HotSingerEntity;
import com.chat.datea.utils.fragment.ReplaceFragment;
import com.chat.datea.utils.volley.VolleySingleton;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 周橹 on 15/10/20.
 */
public class SingerFragment extends Fragment implements View.OnClickListener {

    private View view;
    private ViewPager viewPager;
    private VolleySingleton volleySingleton;
    private SingerViewPagerAdapter singerViewPagerAdapter;
    private List<HotSingerEntity.ArtistEntity> artistEntities;
    private ListView singer_listView;
    private TextView singer_more;
    private HotSingerFragment hotSingerFragment;
    private int i[] = {6, 6, 6, 3, 3, 3, 7, 7, 7, 60, 60, 60, 5};
    private String url = "http://tingapi.ting.baidu.com/v1/restserver/ting?from=android&version=5.6.5.0&method=baidu.ting.artist.getList&format=json&offset=0&limit=30&order=1&area=0&sex=0";

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.singer_layout, null);
        volleySingleton = VolleySingleton.getVolleySingleton(getActivity());
        initViewPagerData();
        initView();
        return view;
    }

    private void initView() {
        View headerView = LayoutInflater.from(getActivity()).inflate(R.layout.singer_deadview,null);
        viewPager = (ViewPager) headerView.findViewById(R.id.singer_viewpager);

        singer_listView = (ListView) view.findViewById(R.id.singer_listView);
        singer_listView.addHeaderView(headerView);

        SingerListViewAdapter singerListViewAdapter = new SingerListViewAdapter(getActivity());

        singer_listView.setAdapter(singerListViewAdapter);
        singer_listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                int ii = i[position-1];

                SingerListFragment listFragment = new SingerListFragment(
                        "http://tingapi.ting.baidu.com/v1/restserver/ting?from=android&version=5.6.5.0&method=baidu.ting.artist.getList&format=json&offset=90&limit=30&order=1&area="
                                + ii + "&sex=" + (((position -1) % 3) + 1));
                new ReplaceFragment(listFragment, getActivity().getSupportFragmentManager(), R.id.fl_content, true, null);

            }
        });
        singer_more = (TextView) headerView.findViewById(R.id.singer_more);
        singer_more.setOnClickListener(this);
    }

    private void initViewPagerData() {
        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                artistEntities = new ArrayList<>();
                HotSingerEntity hotSingerEntity = gson.fromJson(response, HotSingerEntity.class);
                Log.e("chouru", hotSingerEntity + "");
                artistEntities = hotSingerEntity.getArtist();
                singerViewPagerAdapter = new SingerViewPagerAdapter(getActivity(), artistEntities);
                viewPager.setAdapter(singerViewPagerAdapter);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        });
        volleySingleton.addQueue(request, "SingerFragment");
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.singer_more:

                hotSingerFragment = new HotSingerFragment();
                new ReplaceFragment(hotSingerFragment, getActivity().getSupportFragmentManager(), R.id.fl_content, true, null);

                break;
//            case R.id.chinese_singer_male:
//                Intent intent = new Intent(getActivity(), SingerIntroActivity.class);
//                startActivity(intent);
//                break;
        }
    }
}
